/**
 * 机构数据表格
 */
Ext.namespace("Ext.ux.grid");

Ext.ux.grid.GroupGrid = Ext.extend(Ext.ux.grid.CrudGrid, {
	//是否启用行双击事件的事件处理函数，取值为true，为grid添加默认的行双击事件处理函数，取值为false不添加
	enabledDefaultRowDblclickEvent: false,
	initComponent: function(){
		
		var gridStructure = [{ 
			name: 'groupId',
			hidden: true
		},{
			name: 'groupName',  
			header: '工作组名称',
			sortable: true,
			width: 20
		},{
			name: 'orgName',  
			header: '隶属机构',
			sortable: false,
			width: 30
		},{
			name: 'parentGroupName',  
			header: '父工作组',
			sortable: false,
			width: 20
		},{
			name: 'groupLevel',  
			header: '工作组层次',
			sortable: true,
			width: 10
		},{
			name: 'groupTypeLabel',  
			header: '工作组类型',
			sortable: true,
			width: 20
		},{
			name: 'groupStatusLabel',  
			header: '工作组状态',
			sortable: true,
			width: 20
		},{
			name: 'startDate',  
			header: '有效开始日期',
			sortable: true,
			width: 20
		},{
			name: 'endDate',  
			header: '有效结束日期',
			sortable: true,
			width: 10
		}];
		
		this.structure = gridStructure;
		this.defaultSortField = 'groupName';
		this.keyField = 'groupId';
		
		this.deleteMsg = '确认要删除选中的工作组吗？<br/><font color="red">注意</font>：删除工作组将同时删除该工作组下的所有子工作组、岗位以及用户关联信息。';
		this.deleteMsgBoxWidth = 530;
		
		Ext.ux.grid.GroupGrid.superclass.initComponent.call(this);
		
		//如果启用了行双击事件的处理函数
		if(this.enabledDefaultRowDblclickEvent){
			this.on('rowdblclick', function(groupGrid){
				var record = groupGrid.getSelectionModel().getSelected();
				if(record != null){
					var groupId = record.get('groupId');
					this.showGroupInfo(groupId);
				}
			});
		}
		
		
		this.on('updateRecord', function(groupId){
			this.showGroupInfo(groupId);
		});
		this.on('deleteRecord', function(grid, groupIds){
			this.deleteGroup(groupIds);
		});
	},
	//添加顶级工作组
	addTopGroup: function(){
		var win = new Ext.ux.org.omgroup.GroupDetailWindow({id:'groupDetailWindow', title: '新增工作组'});
		win.show();
	},
	//指定机构添加工作组
	orgAddGroup: function(parentOrgId, parentOrgName){
		var win = new Ext.ux.org.omgroup.GroupDetailWindow({id:'groupDetailWindow', title: '新增工作组'});
		win.initParentOrg(parentOrgId, parentOrgName);
		win.show();
	},
	//工作组添加子工作组
	groupAddChildGroup: function(parentOrgId, parentOrgName, parentGroupId, parentGroupName){
		var win = new Ext.ux.org.omgroup.GroupDetailWindow({
				id:'groupDetailWindow', 
				title: '新增工作组'
		});
		win.initParentOrg(parentOrgId, parentOrgName);
		win.initParentGroup(parentGroupId, parentGroupName);
		win.show();	
	},
	//删除工作组
	deleteGroup: function(groupIds){
		Ext.Ajax.request({
			url: 'om-group!delete.action',
			success: function(){
				this.store.reload();
			},
			failure: function(){
				Ext.Msg.alert('系统提示', '删除机构失败！');
			},
			method: 'POST',
			params:{
				groupIds: groupIds
			},
			waitMsg : '正在删除数据，稍候...',
			scope: this
		});
	},
	//显示工作组的信息
	showGroupInfo: function(groupId){
		var inputWin = new Ext.ux.org.omgroup.GroupDetailWindow({id:'groupDetailWindow',title: '修改工作组', groupId: groupId});
		inputWin.getGroupDetailPanel().loadCurGroupInfo(groupId);
		inputWin.show();
	}
	
});